package com.itcast.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.health.mapper.CheckItemMapper;
import com.itcast.health.pojo.CheckItem;
import com.itcast.health.response.entity.PageResult;
import com.itcast.health.service.CheckItemService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckitemServiceImpl implements CheckItemService {
    private final static Logger LOGGER = Logger.getLogger(CheckitemServiceImpl.class);

    @Autowired
    private CheckItemMapper checkItemMapper;
    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @Override
    public CheckItem findOne(Integer id) {
        CheckItem checkItem = checkItemMapper.findOne(id);
        if (checkItem==null){
            LOGGER.error("CheckitemServiceImpl findOne checkItem not exist");
            throw new RuntimeException("检查项不存在");
        }
        return checkItem;
    }

    /**
     * 添加
     *
     * @param checkItem
     */
    @Override
    @Transactional
    public CheckItem add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
        if (checkItem==null||checkItem.getId()==null){
            LOGGER.error("CheckitemServiceImpl add id not exist");
            throw new RuntimeException("添加失败");
        }
        return checkItem;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    @Transactional
    public void delete(Integer id) {
        checkItemMapper.delete(id);
    }

    /**
     * 更新
     *
     * @param checkItem
     */
    @Override
    @Transactional
    public void update(Integer id,CheckItem checkItem) {
        if (id==null){
            LOGGER.error("CheckitemServiceImpl update id not exist");
            throw new RuntimeException("id不存在,更新失败");
        }
        checkItem.setId(id);
        checkItemMapper.update(checkItem);
    }

    /**
     * 条件查询,带分页
     *
     * @param checkItem
     * @return
     */
    @Override
    public PageResult<CheckItem> search(CheckItem checkItem,Integer pageSize,Integer currentPage) {
        if (pageSize==null||pageSize<=0){
            pageSize=5;
        }
        if (currentPage==null||currentPage<=0){
            currentPage=1;
        }
        PageHelper.startPage(pageSize,currentPage);
        Page<CheckItem> result = (Page<CheckItem>) checkItemMapper.search(checkItem);
        long total = result.getTotal();
        if (total==0){
            LOGGER.error("CheckitemServiceImpl search total equal 0");
            throw new RuntimeException("没有记录");
        }
        PageResult<CheckItem> pageResult = new PageResult<>();
        pageResult.setRows(result.getResult());
        pageResult.setTotal(total);
        pageResult.setTotalPage(result.getPages());
        return pageResult;
    }
}
